package com.cheng.admin.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cheng.admin.model.Address;
import com.cheng.admin.service.AddressService;
import com.cheng.common.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 地址 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    AddressService addressService;

    /**
     * <span>@Validated</span>做表单校验
     *
     * @param address 地址表单
     * @return
     */
    @PostMapping("save")
    public R<Boolean> save(@RequestBody @Validated Address address) {
        address.setUserId(StpUtil.getLoginIdAsLong());
        addressService.saveOrUpdate(address);
        return R.ok(true);
    }

    @GetMapping("list")
    public R<List<Address>> listAddress() {
        List<Address> list = addressService.list(Wrappers.<Address>lambdaQuery().eq(Address::getUserId, StpUtil.getLoginIdAsLong()));
        return R.ok(list);
    }

    @GetMapping("get")
    public R<Address> get(@RequestParam Long id) {
        return R.ok(addressService.getById(id));
    }

    @GetMapping("delete")
    public R<Boolean> delete(@RequestParam Long id) {
        addressService.removeById(id);
        return R.ok(true);
    }
}

